/*  File Name	: LocalDB.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-12-13
 *  Description:
 */
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include "MySqlDB.h"
#include "Logger.h"
#include "Cmd.h"
#include "DBRowName.h"
#include "Configurator.h"
#include "ComDefine.h"
#include "EnginePlayer.h"
#include "ChatMonitor.h"

#include "cppconn/prepared_statement.h"
#include "cppconn/metadata.h"
#include "cppconn/resultset_metadata.h"
#include "cppconn/exception.h"
#include "cppconn/warning.h"

/*
 * if success,return role id,else return error code
 */
int MySqlDB::create_local_role(int roleid,std::string account,std::string pwd,int sex)
{
	try
	{
		MyConnection conn = this->get_conn();
		if (NULL == conn.conn_)
		{
			return -1;
		}
		sql::Statement *state = conn.conn_->createStatement();
		if (NULL == state)
		{
			return -1;
		}

		char sql[512] = { 0 };
		std::string sql_str =
				CONFIG_INSTANCE->sql_cfg()[SQL::CREATE_LACCOUNT].asString();
		int chips = 0;
#ifdef DEBUG
		chips = 10000;
#endif
		sprintf(sql, sql_str.c_str(), roleid,account.c_str(), "", "",sex,chips);
		int result = state->execute(sql);
		if (result < 0)
		{
			this->destory(NULL, state);
			return ERROR_SERVER_PROBLEM;
		}
		this->destory(NULL, state);
		return 0;
	}
	catch (SQLException& e)
	{
		LOG((ERROR_LOG,"MySqlDB::create_local_role-ErrorCode:[%d],SQLState:[%s] description:[%s]",
				e.getErrorCode(),e.getSQLState().c_str(),e.what()));
	}
	return -1;
}

int MySqlDB::create_name_key(std::string account)
{
	try
	{
		MyConnection conn = this->get_conn();
		if (NULL == conn.conn_)
		{
			return -1;
		}
		sql::Statement *state = conn.conn_->createStatement();
		if (NULL == state)
		{
			return -1;
		}

		char sql[512] = { 0 };
		std::string sql_str =
				CONFIG_INSTANCE->sql_cfg()[SQL::CREATE_NAMEKEY].asString();

		sprintf(sql, sql_str.c_str(), account.c_str(),"",0);
		int result = state->execute(sql);
		if (result < 0)
		{
			return ERROR_SERVER_PROBLEM;
		}

		int role_id = 0;
		std::string query = "select LAST_INSERT_ID() max_id;";
		ResultSet *rs = state->executeQuery(query);
		if (NULL == rs || rs->rowsCount() <= 0)
		{
			this->destory(rs, state);
			return -1;
		}

		if (rs->next())
		{
			role_id = rs->getInt("max_id");
		}
		this->destory(rs, state);
		return role_id;
	}
	catch (SQLException& e)
	{
		LOG((ERROR_LOG,"MySqlDB::create_name_key-ErrorCode:[%d],SQLState:[%s] description:[%s]",
				e.getErrorCode(),e.getSQLState().c_str(),e.what()));
	}
	return -1;
}

int MySqlDB::delete_name_key(int roleid)
{
	try
	{
		MyConnection conn = this->get_conn();
		if (NULL == conn.conn_)
		{
			return -1;
		}
		sql::Statement *state = conn.conn_->createStatement();
		if (NULL == state)
		{
			return -1;
		}

		std::stringstream q_stream;
		q_stream << "delete from name_key where roleid='" << roleid << "';";

		int result = state->execute(q_stream.str());
		if (result < 0)
		{
			return ERROR_SERVER_PROBLEM;
		}
		this->destory(NULL, state);
		return 0;
	} catch (SQLException& e)
	{
		LOG((ERROR_LOG,"MySqlDB::create_name_key-ErrorCode:[%d],SQLState:[%s] description:[%s]",
				e.getErrorCode(),e.getSQLState().c_str(),e.what()));
	}
	return -1;
}

int MySqlDB::save_name_key(DBPlayer *player)
{
	try
	{
		MyConnection conn = this->get_conn();
		if (NULL == conn.conn_)
		{
			return -1;
		}
		sql::Statement *state = conn.conn_->createStatement();
		if (NULL == state)
		{
			return -1;
		}

		std::string sql_str="update name_key set ip='%s',port='%d' where roleid='%d';";
		char sql[256]={0};
		sprintf(sql,sql_str.c_str(),player->dbip.c_str(),player->dbport,player->role_id());

		int result = state->execute(sql);
		if (result < 0)
		{
			return ERROR_SERVER_PROBLEM;
		}
		this->destory(NULL, state);
		return 0;
	} catch (SQLException& e)
	{
		LOG((ERROR_LOG,"MySqlDB::save_name_key-ErrorCode:[%d],SQLState:[%s] description:[%s]",
				e.getErrorCode(),e.getSQLState().c_str(),e.what()));
	}
	return -1;
}
